package com.yj.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.yj.entity.InstanceSaveReq;
import com.yj.entity.InstanceSaveResp;
import com.yj.exception.BusinessException;
import com.yj.exception.BusinessExceptionEnums;
import com.yj.exception.EntityException;
import com.yj.exception.EntityExceptionEnums;
import com.yj.model.comm.BaseReq;
import com.yj.model.comm.BaseResp;
import com.yj.model.comm.RespHeader;
import com.yj.model.comm.ResultUtil;
import com.yj.model.resource.ResourceInfo;
import com.yj.model.resource.ResourceInfoQueryBO;
import com.yj.model.resource.ResourceTemplate;
import com.yj.service.ResourceService;
import com.yj.utils.CommUtilForEntity;

@RestController
public class ResourceController {

	private Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private ResourceService resourceService;

	@RequestMapping(value = "/ResourceController/getTemplates", method = RequestMethod.POST)
	public BaseResp getTemplates(@RequestBody BaseReq<ResourceTemplate> baseReq) {
		BaseResp baseResp = new BaseResp();
		RespHeader respHeader = new RespHeader();
		List<Object> list = null;
		try {
			ResourceTemplate resourceTemplate = baseReq.getContent();
			list = resourceService.getTemplates(resourceTemplate);
			respHeader.setRespCode(BusinessExceptionEnums.SUCCESS.getCode());
			respHeader.setRespDesc(BusinessExceptionEnums.SUCCESS.getMessage());
		} catch (EntityException e) {
			log.error(e.getMessage(), e);
			respHeader.setRespDesc(e.getMessage());
			respHeader.setRespCode(e.getErrorCode());
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			respHeader.setRespDesc(EntityExceptionEnums.TEMPLATES_ERR.getMessage());
			respHeader.setRespCode(EntityExceptionEnums.TEMPLATES_ERR.getCode());
		}
		baseResp.setHeader(respHeader);
		baseResp.setContent(list);
		return baseResp;
	}

	/**
	 * 查询资源实例列表
	 */
	@RequestMapping(value = "/ResourceController/listInstances", method = RequestMethod.POST)
	public BaseResp listInstances(@RequestBody BaseReq<ResourceInfoQueryBO> baseReq) {
		BaseResp baseResp = new BaseResp();
		RespHeader respHeader = new RespHeader();
		ResultUtil util = new ResultUtil();
		respHeader.setRespCode(BusinessExceptionEnums.SUCCESS.getCode());
		respHeader.setRespDesc(BusinessExceptionEnums.SUCCESS.getMessage());
		try {
			baseReq.getContent().initPage();
			util = resourceService.listInstances(baseReq.getContent());
			baseResp.setContent(util.getResult());
		} catch (BusinessException e) {
			log.error(e.getMessage(), e);
			respHeader.setRespDesc(e.getMessage());
			respHeader.setRespCode(e.getErrorCode());
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			util.setSuccess(false);
			util.setMsg(BusinessExceptionEnums.RESOURCE_LIST_ERR.getMessage());
			respHeader.setRespCode(BusinessExceptionEnums.RESOURCE_LIST_ERR.getCode());
			respHeader.setRespDesc(BusinessExceptionEnums.RESOURCE_LIST_ERR.getMessage());
		}
		baseResp.setHeader(respHeader);
		return baseResp;
	}

	@RequestMapping(value = "/ResourceController/getInstanceDetail", method = RequestMethod.POST)
	public BaseResp getInstanceDetail(@RequestBody BaseReq<ResourceInfo> baseReq) {
		BaseResp baseResp = new BaseResp();
		RespHeader respHeader = new RespHeader();
		InstanceSaveResp instanceSaveResp = new InstanceSaveResp();
		ResourceInfo resourceInfo = baseReq.getContent();
		try {
			if (StringUtils.isBlank(resourceInfo.getInstanceId())) {
				// 说明是新增(只查询属性)
				instanceSaveResp = resourceService.getTemplateAttrCfg(resourceInfo.getTemplateId());
			} else {
				// 修改或者详情（查询属性跟属性值）
				instanceSaveResp = resourceService.getEntityAttrValue(resourceInfo.getInstanceId());
			}
			respHeader.setRespCode(BusinessExceptionEnums.SUCCESS.getCode());
			respHeader.setRespDesc(BusinessExceptionEnums.SUCCESS.getMessage());
		} catch (EntityException e) {
			log.error(e.getMessage(), e);
			respHeader.setRespDesc(e.getMessage());
			respHeader.setRespCode(e.getErrorCode());
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			respHeader.setRespDesc(BusinessExceptionEnums.RESOURCE_DETAIL_ERR.getMessage());
			respHeader.setRespCode(BusinessExceptionEnums.RESOURCE_DETAIL_ERR.getCode());
		}
		baseResp.setHeader(respHeader);
		baseResp.setContent(instanceSaveResp);
		return baseResp;
	}

	@RequestMapping(value = "/ResourceController/saveInstance", method = RequestMethod.POST)
	public BaseResp saveInstance(@RequestBody BaseReq<InstanceSaveReq> baseReq, HttpServletRequest request) {
		BaseResp baseResp = new BaseResp();
		RespHeader respHeader = new RespHeader();
		InstanceSaveReq instanceSaveReq = baseReq.getContent();
		ResourceInfo instanceInfo = (ResourceInfo) CommUtilForEntity.parseContent(instanceSaveReq.getInstanceInfo(),
				ResourceInfo.class);
		try {
			instanceSaveReq.setInstanceInfo(instanceInfo);
			resourceService.saveResource(instanceSaveReq);
			respHeader.setRespCode(BusinessExceptionEnums.SUCCESS.getCode());
			respHeader.setRespDesc(BusinessExceptionEnums.SUCCESS.getMessage());
		} catch (BusinessException e) {
			log.error(e.getMessage(), e);
			respHeader.setRespCode(e.getErrorCode());
			respHeader.setRespDesc(e.getMessage());
		} catch (EntityException e) {
			log.error(e.getMessage(), e);
			respHeader.setRespCode(e.getErrorCode());
			respHeader.setRespDesc(e.getMessage());
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			respHeader.setRespCode(BusinessExceptionEnums.RESOURCE_SAVE_ERR.getCode());
			respHeader.setRespDesc(BusinessExceptionEnums.RESOURCE_SAVE_ERR.getMessage());
		}
		baseResp.setHeader(respHeader);
		return baseResp;
	}

	@RequestMapping(value = "/ResourceController/changeInstanceStatus", method = RequestMethod.POST)
	public BaseResp changeInstanceStatus(@RequestBody BaseReq<ResourceInfo> baseReq) {
		BaseResp baseResp = new BaseResp();
		RespHeader respHeader = new RespHeader();
		try {
			ResourceInfo resourceInfo = baseReq.getContent();
			resourceService.changeResourceStatus(resourceInfo);
			respHeader.setRespCode(BusinessExceptionEnums.SUCCESS.getCode());
			respHeader.setRespDesc(BusinessExceptionEnums.SUCCESS.getMessage());
		} catch (EntityException e) {
			log.error(e.getMessage(), e);
			respHeader.setRespDesc(e.getMessage());
			respHeader.setRespCode(e.getErrorCode());
		} catch (BusinessException e) {
			log.error(e.getMessage(), e);
			respHeader.setRespCode(e.getErrorCode());
			respHeader.setRespDesc(e.getMessage());
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			respHeader.setRespDesc(EntityExceptionEnums.STATUS_ERROR.getMessage());
			respHeader.setRespCode(EntityExceptionEnums.STATUS_ERROR.getCode());
		}
		baseResp.setHeader(respHeader);
		return baseResp;
	}
}